package com.example.bgdharaniPlayer.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.bgdharaniPlayer.R;
import com.example.bgdharaniPlayer.model.AudioTrack;

import java.io.File;
import java.util.List;

public class TrackTableAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    private final List<AudioTrack> tracks;
    private final Context context;
    private int currentPlayingPosition = -1;
    private OnTrackClickListener listener;
    private OnTrackLongClickListener longClickListener;

    public interface OnTrackClickListener {
        void onTrackClick(int position);
    }
    
    public interface OnTrackLongClickListener {
        boolean onTrackLongClick(int position);
    }

    public TrackTableAdapter(Context context, List<AudioTrack> tracks) {
        this.context = context;
        this.tracks = tracks;
    }

    public void setOnTrackClickListener(OnTrackClickListener listener) {
        this.listener = listener;
    }
    
    public void setOnTrackLongClickListener(OnTrackLongClickListener listener) {
        this.longClickListener = listener;
    }

    public void setCurrentPlayingPosition(int position) {
        int oldPosition = currentPlayingPosition;
        currentPlayingPosition = position;
        
        if (oldPosition >= 0) {
            notifyItemChanged(oldPosition + 1); // +1 because of header
        }
        if (currentPlayingPosition >= 0) {
            notifyItemChanged(currentPlayingPosition + 1); // +1 because of header
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_track_table_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_track_table, parent, false);
            return new ItemViewHolder(view);
        }
    }

    private String playlistName = "文件名";
    
    public void setPlaylistName(String name) {
        if (name != null && !name.isEmpty()) {
            this.playlistName = name;
            notifyItemChanged(0); // 更新标题行
        }
    }
    
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            // 设置标题为播放列表名称
            HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
            headerHolder.tvTitleHeader.setText(playlistName);
        } else if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemHolder = (ItemViewHolder) holder;
            int trackPosition = position - 1; // Adjust for header
            AudioTrack track = tracks.get(trackPosition);
            
            itemHolder.tvTrackNumber.setText(String.valueOf(trackPosition + 1));
            
            // 获取文件名（不包含路径）
            String fileName = new File(track.getPath()).getName();
            itemHolder.tvTrackTitle.setText(fileName);
            
            itemHolder.tvPlaybackSpeed.setText(String.format("%.1fx", track.getPlaybackSpeed()));
            itemHolder.tvRepeatCount.setText(String.valueOf(track.getRepeatCount()));
            
            // 显示当前播放指示器
            if (trackPosition == currentPlayingPosition) {
                itemHolder.viewNowPlaying.setVisibility(View.VISIBLE);
            } else {
                itemHolder.viewNowPlaying.setVisibility(View.GONE);
            }
            
            // 设置点击监听器
            itemHolder.itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onTrackClick(trackPosition);
                }
            });
            
            // 设置长按监听器
            itemHolder.itemView.setOnLongClickListener(v -> {
                if (longClickListener != null) {
                    return longClickListener.onTrackLongClick(trackPosition);
                }
                return false;
            });
        }
    }

    @Override
    public int getItemCount() {
        return tracks.size() + 1; // +1 for header
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_HEADER : TYPE_ITEM;
    }

    static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitleHeader;
        
        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitleHeader = itemView.findViewById(R.id.tvTitleHeader);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvTrackNumber;
        TextView tvTrackTitle;
        TextView tvPlaybackSpeed;
        TextView tvRepeatCount;
        View viewNowPlaying;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTrackNumber = itemView.findViewById(R.id.tvTrackNumber);
            tvTrackTitle = itemView.findViewById(R.id.tvTrackTitle);
            tvPlaybackSpeed = itemView.findViewById(R.id.tvPlaybackSpeed);
            tvRepeatCount = itemView.findViewById(R.id.tvRepeatCount);
            viewNowPlaying = itemView.findViewById(R.id.viewNowPlaying);
        }
    }
}